package com.zqxq.lottery.controller.order;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zqxq.common.constant.Result;
import com.zqxq.common.enums.CodeEnum;
import com.zqxq.common.utils.StrUtils;
import com.zqxq.lottery.entity.po.export.ExpertInfoPO;
import com.zqxq.lottery.entity.po.order.FollowOrderPO;
import com.zqxq.lottery.entity.vo.expert.ExpertDetailVO;
import com.zqxq.lottery.entity.vo.order.FollowOrderVO;
import com.zqxq.lottery.entity.vo.order.PushOrderDetailVO;
import com.zqxq.lottery.entity.vo.order.PushOrderVO;
import com.zqxq.lottery.service.expert.IExpertInfoService;
import com.zqxq.lottery.service.order.IFollowOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 苦智
 * @date 2018/8/3 17:45
 * @description 跟单功能
 * @ClassName FollowOrderController
 */
/*@RequestMapping(value = "/follow/order/getList")
    Result getFollowOrder(@RequestParam("userId") String userId,@RequestParam("type") Integer type);*/
@RestController
@RequestMapping("/follow")
@Slf4j
public class FollowOrderController {
    @Autowired
    private IFollowOrderService followOrderService;
    @Autowired
    private IExpertInfoService expertInfoService;

    /**
    * @Author 苦智
    * @Description 用户跟单列表
    * @Date 17:56 2018/8/3
    * @Param [userId, type] 用户id、类型 1:人气2：命中3：盈利4：关注
    * @return com.zqxq.common.constant.Result
    **/
    @PostMapping("/order/getList")
    public Result getFollowOrder(String userId, String orderId, Integer type, Integer pageNo, Integer pageSize,String followUserId){
        log.info("用户跟单列表userId,orderId,type,pageNo,pageSize",userId,orderId,type,pageNo,pageSize);
        if (type == 4 && StrUtils.isEmpty(followUserId))
            return new Result(CodeEnum.FAIL_100002.getCode(),CodeEnum.FAIL_100002.getMsg());
        Result result = new Result();
        Page<FollowOrderVO> page = new Page(pageNo,pageSize);
        List<FollowOrderVO> orderList = followOrderService.getFollowOrderList(userId,orderId,type,followUserId,page);
        page.setRecords(orderList);
        result.setCode(CodeEnum.SUCCESS.getCode());
        result.setData(page);
        return result;
    }
    /**
    * @Author 苦智
    * @Description 方案详情
    * @Date 9:42 2018/8/7
    * @Param [orderId] 订单id
    * @return com.zqxq.common.constant.Result
    **/
    @PostMapping("/order/programmeDetai")
    public Result getProgrammeDetail(String orderId,String expertId,String followUserId){
        Result result = new Result();
        if(StrUtils.isEmpty(orderId) || StrUtils.isEmpty(expertId)){
            result.setCode(CodeEnum.FAIL_100002.getCode());
            result.setMsg(CodeEnum.FAIL_100002.getMsg());
        }
        Wrapper<FollowOrderPO> wrapper = new EntityWrapper();
        wrapper.eq("order_id",orderId);
        FollowOrderPO followOrderPO = followOrderService.selectOne(wrapper);
        if (followOrderPO == null){
            result.setCode(CodeEnum.FAIL_100002.getCode());
            result.setData("跟单不存在");
            return result;
        }
        ExpertDetailVO expertDetailVO = followOrderService.getPushOrderDetailByPid(orderId,expertId,followUserId);
        result.setCode(CodeEnum.SUCCESS.getCode());
        result.setData(expertDetailVO);
        return result;
    }

    /**
    * @Author 苦智
    * @Description 专家详情
    * @Date 9:42 2018/8/7
    * @Param [userId] 用户id
    * @return com.zqxq.common.constant.Result
    **/
    @PostMapping("/order/expertDetail")
    public Result getExpertDetail(String userId,String followUserId){
        log.info("专家详情,userId={},followUserId={}",userId,followUserId);
        Result result = new Result();
        if (StrUtils.isEmpty(userId) || StrUtils.isEmpty(followUserId)){
            result.setCode(CodeEnum.FAIL_100002.getCode());
            result.setMsg(CodeEnum.FAIL_100002.getMsg());
            return result;
        }
        Wrapper<ExpertInfoPO> wrapper = new EntityWrapper();
        wrapper.eq("user_id",userId);
        ExpertInfoPO expertInfoPO = expertInfoService.selectOne(wrapper);
        if (expertInfoPO == null){
            result.setCode(CodeEnum.FAIL_100002.getCode());
            result.setMsg("专家不存在");
            return result;
        }
        ExpertDetailVO detail = followOrderService.getExpertDetail(userId,followUserId);
        result.setCode(CodeEnum.SUCCESS.getCode());
        result.setData(detail);
        return result;
    }

    /**
    * @Author 苦智
    * @Description 专家推单列表
    * @Date 11:08 2018/8/8
    * @Param [userId,] type：1：近7日2：近30日、status：1：中奖推单2：待开推单3：未中推单
    * @return com.zqxq.common.constant.Result
    **/
    @PostMapping("/order/pushOrderList")
    public Result getPushOrderList(String userId,Integer type,Integer status,Integer pageNo,Integer pageSize){
        if (StrUtils.isEmpty(userId))
            return new Result(CodeEnum.FAIL_100002.getCode(),"专家id不能为空");
        PushOrderVO pushOrderVO = followOrderService.getPushOrder(userId,type,status,pageNo,pageSize);
        return new Result(CodeEnum.SUCCESS.getCode(),CodeEnum.SUCCESS.getMsg(),pushOrderVO);
    }

}
